﻿/***************************************************
//  Copyright (c) Premium Tax Free 2011
***************************************************/

using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows.Forms;
using PremierTaxFree.PTFLib;
using PremierTaxFree.PTFLib.Data;
using PremierTaxFree.PTFLib.Extensions;
using PremierTaxFree.PTFLib.Web;

namespace Downloader
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                    throw new ArgumentException("Wrong number of arguments");

                DataTable table = DataAccess.GetDatabases(false);
                if (!table.Select("database_name='LocalDB'").Exists())
                    DataAccess.CreateDb_Client();

                Console.ForegroundColor = ConsoleColor.White;

                Version clientVersion = new Version(args[0]);

                ReceivingWebService client = new ReceivingWebService();
                client.SetUrl(ConfigurationManager.AppSettings[Strings.All_CentralServerUrl]);
                client.SetCreditails("rrossenbg", Strings.PASS, null);

                client.Test();

                Console.WriteLine("Getting version information");
                Version serverVersion = new Version(client.GetValue(Strings.Transferring_CurrentVersion).ToStringSf());
                if (clientVersion != serverVersion)
                {
                    Console.WriteLine("Versions are different");
                    if (MessageBox.Show(
                        string.Format("Yours version is {0}.\n Server's version is {1}.\n Would you like to get server's version?", clientVersion, serverVersion),
                        "Downloader",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string dir = AppDomain.CurrentDomain.BaseDirectory;
                        Console.WriteLine("Connecting to the central server");
                        Console.WriteLine("Getting file information");
                        string[] files = client.GetFiles();

                        foreach (string file in files)
                        {
                            if (string.Compare(file, "Downloader.exe", true) == 0)
                                continue;

                            if (!file.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase) &&
                                !file.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
                                continue;

                            Console.WriteLine("Downloading {0}", file);

                            try
                            {
                                byte[] buffer = client.GetFile(file);
                                string filePath = Path.Combine(dir, file);
                                File.WriteAllBytes(filePath, buffer);
                            }
                            catch(Exception e)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine(e.Message);
                                Console.ForegroundColor = ConsoleColor.White;
                            }
                        }
                    }
                }
                Console.WriteLine("Done!");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.Read();
            }
        }        
    }
}
